package handler;

import entity.Apply;
import org.apache.commons.dbutils.ResultSetHandler;

import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class CustomBeanApplyListHandler implements ResultSetHandler<List<Apply>> {
    @Override
    public List<Apply> handle(ResultSet resultSet) throws SQLException {
        List<Apply> applyList = new ArrayList<>();
        while (resultSet.next()) {
            Apply apply = new Apply();
            apply.setId(resultSet.getInt("id"));
            apply.setName(resultSet.getString("name"));
            apply.setEmail(resultSet.getString("email"));
            apply.setAge(resultSet.getInt("age"));
            apply.setTelephone(resultSet.getString("telephone"));
            apply.setMessage(resultSet.getString("message"));
            java.util.Date sqlDate = resultSet.getDate("applyTime");
            if (sqlDate != null) {
                apply.setApplyTime(Date.valueOf(((Date) sqlDate).toLocalDate()));
            }else {
                apply.setApplyTime(null);
            }
            apply.setState(resultSet.getInt("state"));
            applyList.add(apply);

        }
        return applyList;
    }
}
